clear 
clear matrix
set memory 1000m
set more off
cap log close

cd "/Users/..."

global do_file="‎⁨/Users/.../do_file"
global log_file="/Users/.../log_file"
global raw_data="/Users/.../raw_data⁩⁩"
global working_data="/Users/.../working_data"
global results="/Users/.../results"
global results="/Users/.../results/072020/main_reg"


log using log_file/reg_day_072020_add.log, replace



******************
******************
* Use Small Sample
******************
******************


use working_data/merge_quote_drive_081619.dta, clear



gen temp=date(vehicle_register_date, "YMD")
bysort user_id: egen car_regis_date=min(temp) 
drop temp

gen temp=substr(vehicle_register_date, 1, 4)
destring temp, replace force
bysort user_id: egen car_regis_year=min(temp)
drop temp

count if car_regis_date==.
tab car_regis_year

replace car_regis_year=. if car_regis_year==1970

* user age
gen temp=substr(user_id_num, 7, 4)
destring temp, replace force
bysort user_id: egen user_birth_year=min(temp)
drop temp

tab user_birth_year
replace user_birth_year=. if user_birth_year<1951

tab user_id_num if user_birth_year==8632, m
replace user_birth_year=. if user_birth_year==8632

tab user_birth_year

* gender
gen temp=substr(user_id_num, 17, 1)
tab temp, m
destring temp, replace force

gen female=1 if temp==0 | temp==2 | temp==4 | temp==6 | temp==8
replace female=0 if temp>0 & temp~=. & female==.

bysort user_id: egen female_new=min(female)
drop temp female
rename female_new female

tab female, m


* car price: use median as a cut-off
gen cheap_car=1 if car_price<=133262
replace cheap_car=0 if car_price>133262 & car_price~=.

tab cheap_car, m


drop if trip_start_date==""

sort user_id trip_start_date
bysort user_id: gen day_index=_n

tsset user_id day_index


** Create measures for NM and generate differenced variables
do do_file/dofile_072020/gen_nm_diff


** summarize average levels of the risky behavior
sum total_phone_use distance duration speed drive_at_night highway
sum total_phone_use distance duration speed drive_at_night highway if female==0


estpost tabstat total_phone_use distance duration speed drive_at_night highway, listwise statistics(mean)
esttab . using $results/avg_values.csv, append cells("total_phone_use distance duration speed drive_at_night highway") noobs
 

estpost tabstat total_phone_use distance duration speed drive_at_night highway if female==0, listwise statistics(mean)
esttab . using $results/avg_values.csv, append cells("total_phone_use distance duration speed drive_at_night highway") noobs
 
 
 





********************************
* Robustness Check: male drivers 
********************************

preserve

keep if female==0


bysort user_id: gen temp=_n
count if temp==1 // only has 171 users in this sample, 18474 obs


* just use this line to start the table
ivregress gmm diff_agg_acc l.diff_hb1_dummy diff_traffic_jam diff_weekend diff_rush_hour ///
diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
(l.diff_agg_acc = l(2).total_agg_acc), vce(cluster user_id) nocons
outreg2 using $results/reg_male_072020.xls, replace keep(l.diff_hb1_dummy) 

** baseline 
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_phone_use = l(2).total_phone_use), vce(cluster user_id) nocons
	outreg2 using $results/reg_male_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_distance = l(2).distance), vce(cluster user_id) nocons
	outreg2 using $results/reg_male_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_duration = l(2).duration), vce(cluster user_id) nocons
	outreg2 using $results/reg_male_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_speed = l(2).speed), vce(cluster user_id) nocons
	outreg2 using $results/reg_male_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_drive_at_night = l(2).drive_at_night), vce(cluster user_id) nocons
	outreg2 using $results/reg_male_072020.xls, append keep(l.`nm')


	ivregress gmm diff_highway l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_highway = l(2).highway), vce(cluster user_id) nocons
	outreg2 using $results/reg_male_072020.xls, append keep(l.`nm')
	
	
}

** IV specification
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_male_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_male_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_male_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_male_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_male_072020.xls, append keep(l.`nm')
	
	
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_male_072020.xls, append keep(l.`nm')
	
	
	
}



restore





***************************************
* Robustness Check: Rural vs Urban Area
***************************************

use working_data/clean_day_level_geo_weather_081519.dta, clear

sort user_id trip_start_date
bysort user_id: gen day_index=_n

tsset user_id day_index

** Create measures for NM and generate differenced variables
do do_file/dofile_072020/gen_nm_diff.do


bysort user_id: egen county_avg=mean(county)
bysort user_id: egen district_avg=mean(district)

gen rural=1 if county_avg>district_avg & county_avg~=.
replace rural=0 if county_avg<=district_avg & district_avg~=.


tab prov, m

gen temp=1 if prov=="上海市" | prov=="北京" | prov=="北京市" | prov=="海淀区" | prov=="广东省" | prov=="江苏省" | prov=="山东省" | prov=="浙江省"
gen temp1=1 if temp==. & prov~=""

bysort user_id: egen temp_total=total(temp)
bysort user_id: egen temp1_total=total(temp1)

gen dev=1 if temp_total>temp1_total & temp_total~=.
replace dev=0 if temp_total<=temp1_total & temp1_total~=.
drop temp-temp1_total

tab dev, m


** summarize average levels of the risky behavior
sum total_phone_use distance duration speed drive_at_night highway
sum total_phone_use distance duration speed drive_at_night highway if rural==1
sum total_phone_use distance duration speed drive_at_night highway if rural==0
sum total_phone_use distance duration speed drive_at_night highway if dev==1
sum total_phone_use distance duration speed drive_at_night highway if dev==0

estpost tabstat total_phone_use distance duration speed drive_at_night highway if rural==1, listwise statistics(mean)
esttab . using $results/avg_values.csv, append cells("total_phone_use distance duration speed drive_at_night highway") noobs
 
estpost tabstat total_phone_use distance duration speed drive_at_night highway if rural==0, listwise statistics(mean)
esttab . using $results/avg_values.csv, append cells("total_phone_use distance duration speed drive_at_night highway") noobs

estpost tabstat total_phone_use distance duration speed drive_at_night highway if dev==1, listwise statistics(mean)
esttab . using $results/avg_values.csv, append cells("total_phone_use distance duration speed drive_at_night highway") noobs

estpost tabstat total_phone_use distance duration speed drive_at_night highway if dev==0, listwise statistics(mean)
esttab . using $results/avg_values.csv, append cells("total_phone_use distance duration speed drive_at_night highway") noobs

** Start regression for rural areas

preserve

keep if rural==1


bysort user_id: gen temp=_n
count if temp==1 


* just use this line to start the table
ivregress gmm diff_agg_acc l.diff_hb1_dummy diff_traffic_jam diff_weekend diff_rush_hour ///
diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
(l.diff_agg_acc = l(2).total_agg_acc), vce(cluster user_id) nocons
outreg2 using $results/reg_rural_072020.xls, replace keep(l.diff_hb1_dummy) 

** baseline 
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_phone_use = l(2).total_phone_use), vce(cluster user_id) nocons
	outreg2 using $results/reg_rural_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_distance = l(2).distance), vce(cluster user_id) nocons
	outreg2 using $results/reg_rural_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_duration = l(2).duration), vce(cluster user_id) nocons
	outreg2 using $results/reg_rural_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_speed = l(2).speed), vce(cluster user_id) nocons
	outreg2 using $results/reg_rural_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_drive_at_night = l(2).drive_at_night), vce(cluster user_id) nocons
	outreg2 using $results/reg_rural_072020.xls, append keep(l.`nm')


	ivregress gmm diff_highway l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_highway = l(2).highway), vce(cluster user_id) nocons
	outreg2 using $results/reg_rural_072020.xls, append keep(l.`nm')
	
	
}


** IV specification
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_rural_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_rural_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_rural_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_rural_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_rural_072020.xls, append keep(l.`nm')
	
	
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_rural_072020.xls, append keep(l.`nm')
	
	
	
}


restore





** Start regression for urban areas

preserve

keep if rural==0


bysort user_id: gen temp=_n
count if temp==1 


* just use this line to start the table
ivregress gmm diff_agg_acc l.diff_hb1_dummy diff_traffic_jam diff_weekend diff_rush_hour ///
diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
(l.diff_agg_acc = l(2).total_agg_acc), vce(cluster user_id) nocons
outreg2 using $results/reg_urban_072020.xls, replace keep(l.diff_hb1_dummy) 

** baseline 
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_phone_use = l(2).total_phone_use), vce(cluster user_id) nocons
	outreg2 using $results/reg_urban_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_distance = l(2).distance), vce(cluster user_id) nocons
	outreg2 using $results/reg_urban_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_duration = l(2).duration), vce(cluster user_id) nocons
	outreg2 using $results/reg_urban_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_speed = l(2).speed), vce(cluster user_id) nocons
	outreg2 using $results/reg_urban_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_drive_at_night = l(2).drive_at_night), vce(cluster user_id) nocons
	outreg2 using $results/reg_urban_072020.xls, append keep(l.`nm')


	ivregress gmm diff_highway l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_highway = l(2).highway), vce(cluster user_id) nocons
	outreg2 using $results/reg_urban_072020.xls, append keep(l.`nm')
	
	
}


** IV specification
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_urban_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_urban_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_urban_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_urban_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_urban_072020.xls, append keep(l.`nm')
	
	
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_urban_072020.xls, append keep(l.`nm')
	
	
	
}


restore






** Start regression for economically developed provinces

preserve

keep if dev==1


bysort user_id: gen temp=_n
count if temp==1 


* just use this line to start the table
ivregress gmm diff_agg_acc l.diff_hb1_dummy diff_traffic_jam diff_weekend diff_rush_hour ///
diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
(l.diff_agg_acc = l(2).total_agg_acc), vce(cluster user_id) nocons
outreg2 using $results/reg_dev_072020.xls, replace keep(l.diff_hb1_dummy) 

** baseline 
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_phone_use = l(2).total_phone_use), vce(cluster user_id) nocons
	outreg2 using $results/reg_dev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_distance = l(2).distance), vce(cluster user_id) nocons
	outreg2 using $results/reg_dev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_duration = l(2).duration), vce(cluster user_id) nocons
	outreg2 using $results/reg_dev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_speed = l(2).speed), vce(cluster user_id) nocons
	outreg2 using $results/reg_dev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_drive_at_night = l(2).drive_at_night), vce(cluster user_id) nocons
	outreg2 using $results/reg_dev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_highway l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_highway = l(2).highway), vce(cluster user_id) nocons
	outreg2 using $results/reg_dev_072020.xls, append keep(l.`nm')
	
	
}


** IV specification
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_dev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_dev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_dev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_dev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_dev_072020.xls, append keep(l.`nm')
	
	
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_dev_072020.xls, append keep(l.`nm')
	
	
	
}


restore


** Start regression for economically less-developed provinces

preserve

keep if dev==0


bysort user_id: gen temp=_n
count if temp==1 


* just use this line to start the table
ivregress gmm diff_agg_acc l.diff_hb1_dummy diff_traffic_jam diff_weekend diff_rush_hour ///
diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
(l.diff_agg_acc = l(2).total_agg_acc), vce(cluster user_id) nocons
outreg2 using $results/reg_ldev_072020.xls, replace keep(l.diff_hb1_dummy) 

** baseline 
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_phone_use = l(2).total_phone_use), vce(cluster user_id) nocons
	outreg2 using $results/reg_ldev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_distance = l(2).distance), vce(cluster user_id) nocons
	outreg2 using $results/reg_ldev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_duration = l(2).duration), vce(cluster user_id) nocons
	outreg2 using $results/reg_ldev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_speed = l(2).speed), vce(cluster user_id) nocons
	outreg2 using $results/reg_ldev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_drive_at_night = l(2).drive_at_night), vce(cluster user_id) nocons
	outreg2 using $results/reg_ldev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_highway l.`nm' diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny diff_rain diff_snow l.diff_drive_score diff_gap_time ///
	(l.diff_highway = l(2).highway), vce(cluster user_id) nocons
	outreg2 using $results/reg_ldev_072020.xls, append keep(l.`nm')
	
	
}


** IV specification
foreach nm of varlist diff_hb1_dummy-diff_hb9{

	

	ivregress gmm diff_phone_use diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_phone_use l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).total_phone_use l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_ldev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_distance diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_distance l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).distance l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_ldev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_duration diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_duration l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).duration l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_ldev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_speed diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_speed l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).speed l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_ldev_072020.xls, append keep(l.`nm')


	ivregress gmm diff_drive_at_night diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_drive_at_night l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).drive_at_night l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_ldev_072020.xls, append keep(l.`nm')
	
	
	
	ivregress gmm diff_highway diff_traffic_jam diff_weekend diff_rush_hour ///
	diff_high_temper diff_low_temper diff_sunny  diff_rain diff_snow ///
	(l.diff_highway l.`nm' l.diff_drive_score diff_gap_time ///
	= l(2).highway l(2).drive_score l(2).gap_time l(2).weekend l(2).rush_hour ///
	l(2).total_traffic_sec l(2).high_temper l(2).low_temper l(2).sunny l(2).rain l(2).snow), vce(cluster user_id) nocons
	outreg2 using $results/reg_ldev_072020.xls, append keep(l.`nm')
	
	
	
}


restore
